//
//  orderFood.swift
//  HanMeiMei
//
//  Created by xiaoyu on 2019/1/24.
//

import Foundation

var globalFoodNotifyDictionary : Dictionary<String, Bool> = Dictionary.init()

private let foodUrls = ["https://oapi.dingtalk.com/robot/send?access_token=704d34aa98dd521c77dbde6f6700866c353a7caceedb222ad2b23b266999b5e0",] as Array

private let foodSession: URLSession = URLSession.init(configuration: .default)

private func sendOrderFoodRequest(time: String,final: Bool) -> Void {
    print("Starting Notify Request...")
    
    //    let url = "https://oapi.dingtalk.com/robot/send?access_token=65475953a0e3671de75b361e1c64bb303219ba4e03f8a2d327b60d29a848fb09"
    
    let markdown_content_array = [
        "### 蒸羊羔,蒸熊掌,蒸鹿尾儿 \n ### 烧花鸭,烧雏鸡儿,烧子鹅\n ### 卤煮咸鸭,酱鸡,腊肉,松花,小肚儿\n ### 晾肉,香肠,什锦苏盘,\n ### 熏鸡,白肚儿,清蒸八宝猪,江米酿鸭子,\n ### 罐儿野鸡,罐儿鹌鹑,\n ### 卤什锦,卤子鹅,卤虾",
        "### 烩虾,炝虾仁儿,山鸡,兔脯,菜蟒,银鱼,\n ### 清蒸哈什蚂,烩鸭腰儿,烩鸭条儿,\n ### 清拌鸭丝儿,黄心管儿,\n ### 焖白鳝,焖黄鳝,豆鼓鲇鱼,锅烧鲇鱼,\n ### 烀皮甲鱼,锅烧鲤鱼,抓炒鲤鱼,",
        "### 软炸里脊,软炸鸡,什锦套肠,麻酥油卷儿,\n ### 熘鲜蘑,熘鱼脯儿,熘鱼片儿,\n ### 熘鱼肚儿,醋熘肉片儿,熘白蘑,\n ### 烩三鲜,炒银鱼,烩鳗鱼,清蒸火腿,\n ### 炒白虾,炝青蛤,炒面鱼,\n ### 炝芦笋,芙蓉燕菜,炒肝尖儿,",
        "### 南炒肝关儿,油爆肚仁儿,汤爆肚领儿,\n ### 炒金丝,烩银丝,糖熘饹炸儿,\n ### 糖熘荸荠,蜜丝山药,拔丝鲜桃,\n ### 熘南贝,炒南贝,烩鸭丝,烩散丹,\n ### 清蒸鸡,黄焖鸡,大炒鸡,熘碎鸡,\n ### 香酥鸡,炒鸡丁儿,熘鸡块儿,",
        "### 三鲜丁儿,八宝丁儿,清蒸玉兰片,\n ### 炒虾仁儿,炒腰花儿,炒蹄筋儿,\n ### 锅烧海参,锅烧白菜,\n ### 炸海耳,浇田鸡,桂花翅子,\n ### 清蒸翅子,炸飞禽,炸葱,炸排骨,",
        "### 烩鸡肠肚儿,烩南荠,盐水肘花儿,\n ### 拌瓤子,炖吊子,锅烧猪蹄儿,\n ### 烧鸳鸯,烧百合,烧苹果,\n ### 酿果藕,酿江米,炒螃蟹.氽大甲,\n ### 什锦葛仙米,石鱼,带鱼,",
        "### 黄花鱼,油泼肉,酱泼肉,\n ### 红肉锅子,白肉锅子,菊花锅子,\n ### 野鸡锅子,元宵锅子,\n ### 杂面锅子,荸荠一品锅子,\n ### 软炸飞禽,龙虎鸡蛋,猩唇,",
        "### 驼峰,鹿茸,熊掌,奶猪,奶鸭子,\n ### 杠猪,挂炉羊,清蒸江瑶柱,\n ### 糖熘鸡头米,拌鸡丝儿,拌肚丝儿,\n ### 什锦豆腐,什锦丁儿,精虾,\n ### 精蟹,精鱼,精熘鱼片儿,",
        "### 熘蟹肉,炒蟹肉,清拌蟹肉,\n ### 蒸南瓜,酿倭瓜,炒丝瓜,焖冬瓜,\n ### 焖鸡掌,焖鸭掌,焖笋,熘茭白,\n ### 茄干儿晒卤肉,鸭羹,蟹肉羹,三鲜木樨汤,\n ### 红丸子,白丸子,熘丸子,炸丸子,三鲜丸子,\n ### 四喜丸子,氽丸子,葵花丸子,\n ### 饹炸丸子,豆腐丸子,",
        "### 红炖肉,白炖肉,松肉,扣肉,\n ### 烤肉,酱肉,荷叶卤,一品肉,\n ### 樱桃肉,马牙肉,酱豆腐肉,\n ### 坛子肉,罐儿肉,元宝肉,福禄肉,\n ### 红肘子,白肘子,水晶肘子,",
        "### 蜜蜡肘子,烧烀肘子,扒肘条儿,\n ### 蒸羊肉,烧羊肉,五香羊肉,\n ### 酱羊肉.氽三样儿,爆三样儿,\n ### 烧紫盖儿,炖鸭杂儿,熘白杂碎,\n ### 三鲜鱼翅,栗子鸡,尖氽活鲤鱼,板鸭,筒子鸡",
        "### 咕咾肉木须肉广东叉烧米粉肉\n ### 煲仔饭手抓饭扬州炒饭八宝饭\n ### 水煮鱼红烧鱼西湖醋鱼清蒸鱼\n ### 担担面热干面兰州拉面刀削面\n ### 外国人说肉夹馍是中国的汉堡包\n ### 阳澄湖的大闸蟹香港人最喜欢\n ### 闻着臭吃着香的是长沙的臭豆腐\n ### 辣不怕怕不辣的是重庆的麻辣串\n ### 满汉全席菜式多有一百零八道哇\n ### 萝卜白菜五谷杂粮营养又保健",
        ]
    
    for url in foodUrls {
        var req = URLRequest.init(url: (URL.init(string: url) ?? nil)!)
        req.httpMethod = "POST"
        req.addValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
        
        let timeInContent = time
        
        var noti_content = "@所有人 点餐啦 \(timeInContent)"
        if (final) {
            noti_content = "@所有人 快四点半啦，再不点餐就没啦 \(timeInContent)"
        }
        let textnoti = "{\"msgtype\": \"text\",\"text\": {\"content\": \"\(noti_content)\"},\"at\":{\"atMobiles\": [],\"isAtAll\": true}}"
        req.httpBody = textnoti.data(using: String.Encoding.utf8)
        
        let textTask = foodSession.dataTask(with: req, completionHandler: { (data, resp, error) in
            if (error != nil) {
                print("Notify Request Return Error : \(error!)")
            }
        })
        textTask.resume()
        
        let content_md = markdown_content_array.randomElement()
        
        let markdownnoti = "{\"msgtype\": \"markdown\",\"markdown\": {\"title\":\"点餐点餐点餐\",\"text\":\"#### 点餐啦  \n > \(content_md!)\n\n > ![screenshot](https://img-hxy021.didistatic.com/static/shiyu/1542875851281-ba0dba4a0f854470a5b215f380909ec0.png)\n  > # [点击打开食域](https://shiyu.didichuxing.com/) \"},\"at\": {\"atMobiles\": [],\"isAtAll\": false}}"
        
        req.httpBody = markdownnoti.data(using: String.Encoding.utf8)
        
        let markdownTask = foodSession.dataTask(with: req, completionHandler: { (data, resp, error) in
            if (error != nil) {
                print("Notify Request Return Error : \(error!)")
            }
        })
        markdownTask.resume()
    }
}

public func orderFoodNotify(day: String, hour: Int, minute: Int) -> Void {
    let hour_str = String.init(format: "%02d", hour)
    let minute_str = String.init(format: "%02d", minute)
    let time_str = "\(hour_str):\(minute_str)"
    //    print("Judge Notify at \(day) \(String.init(format: "%02d", hour)):\(String.init(format: "%02d", minute))")
    if (hour > 14 && hour < 17) {
        if (hour == 16) {
            if (minute > 5 && minute < 31) {
                let finalNotifySaveKey = "\(day)\(hour)\(30)"
                
                if (globalFoodNotifyDictionary[finalNotifySaveKey] ?? false == false) {
                    globalFoodNotifyDictionary[finalNotifySaveKey] = true
                    print("Judge Notify -> final notify \(time_str)")
                    sendOrderFoodRequest(time: time_str, final: true)
                } else {
                    //                print("Judge Notify -> forbben")
                }
            }
            return;
        }
    } else {
        //        print("Judge Notify -> forbben")
    }
}
